#include "../include/server_simulator.h"
#include <iostream>
#include <vector>
#include <cstring>

int main() {
    const char* manufacturer = "AI_SERVER_INC";
    const char* sn = "SN20240601";
    int disk_count = 2;
    std::size_t disk_sizes[2] = {1024 * 1024 * 10, 1024 * 1024 * 20}; // 10MB, 20MB

    void* server = server_create(manufacturer, sn, disk_count, disk_sizes);
    if (!server) {
        std::cout << "服务器创建失败！" << std::endl;
        return 1;
    }
    server_power_on(server);
    std::cout << "服务器已上电。" << std::endl;

    char buf[128];
    server_get_manufacturer(server, buf, sizeof(buf));
    std::cout << "制造商: " << buf << std::endl;
    server_get_sn(server, buf, sizeof(buf));
    std::cout << "SN: " << buf << std::endl;
    std::cout << "磁盘数量: " << server_get_disk_count(server) << std::endl;
    for (int i = 0; i < server_get_disk_count(server); ++i) {
        std::cout << "磁盘" << i << "大小: " << server_get_disk_size(server, i) << " 字节" << std::endl;
    }
    server_set_fault(server, 1001);
    std::cout << "故障码: " << server_get_fault_code(server) << std::endl;
    server_clear_fault(server);
    std::cout << "清除故障后故障码: " << server_get_fault_code(server) << std::endl;
    server_power_off(server);
    std::cout << "服务器已下电。" << std::endl;

    // 新增：查询服务器信息
    ServerInfo info;
    if (server_get_info(server, &info) == 0) {
        std::cout << "\n[ServerInfo接口演示]" << std::endl;
        std::cout << "服务器状态: " << (info.powered_on ? "上电" : "下电") << std::endl;
        std::cout << "故障码: " << info.fault_code << std::endl;
        std::cout << "磁盘数量: " << info.disk_count << std::endl;
        for (int i = 0; i < info.disk_count; ++i) {
            std::cout << "磁盘" << i << "容量: " << info.disk_sizes[i] << " 字节" << std::endl;
        }
        std::cout << "制造商: " << info.manufacturer << std::endl;
        std::cout << "SN: " << info.sn << std::endl;
    } else {
        std::cout << "ServerInfo接口调用失败！" << std::endl;
    }

    // 新增：测试通过SN读写磁盘
    std::cout << "\n[通过SN读写磁盘演示]" << std::endl;
    
    // 获取服务器SN
    char server_sn[64];
    server_get_sn(server, server_sn, sizeof(server_sn));
    std::cout << "服务器SN: " << server_sn << std::endl;
    
    // 获取磁盘0的SN
    char disk_sn[64];
    if (server_get_disk_sn(server, 0, disk_sn, sizeof(disk_sn)) == 0) {
        std::cout << "磁盘0 SN: " << disk_sn << std::endl;
        
        // 通过SN写入数据
        const char* test_data = "Test data via SN";
        if (server_write_disk_by_sn(server_sn, disk_sn, 0, test_data, strlen(test_data)+1) == 0) {
            std::cout << "通过SN写入数据成功" << std::endl;
            
            // 通过SN读取数据
            char read_buf[64] = {0};
            if (server_read_disk_by_sn(server_sn, disk_sn, 0, read_buf, strlen(test_data)+1) == 0) {
                std::cout << "通过SN读取数据成功: " << read_buf << std::endl;
            } else {
                std::cout << "通过SN读取数据失败" << std::endl;
            }
        } else {
            std::cout << "通过SN写入数据失败" << std::endl;
        }
        
        // 测试错误的服务器SN
        const char* wrong_server_sn = "WRONG_SN";
        if (server_write_disk_by_sn(wrong_server_sn, disk_sn, 0, test_data, strlen(test_data)+1) != 0) {
            std::cout << "错误的服务器SN测试通过" << std::endl;
        }
        
        // 测试错误的磁盘SN
        const char* wrong_disk_sn = "WRONG_DISK_SN";
        if (server_write_disk_by_sn(server_sn, wrong_disk_sn, 0, test_data, strlen(test_data)+1) != 0) {
            std::cout << "错误的磁盘SN测试通过" << std::endl;
        }
    } else {
        std::cout << "获取磁盘SN失败" << std::endl;
    }

    // 新增：通过server接口直接写盘、再通过SN读盘验证
    std::cout << "\n[直接写盘+通过SN读盘一致性验证]" << std::endl;
    const char* direct_write_data = "DirectWriteTest123";
    int target_disk = 1; // 选择磁盘1做测试
    // 直接写入磁盘1
    if (server_get_disk_count(server) > target_disk) {
        // 先通过server接口写入（直接用C接口）
        void* server_ptr = server;
        // 获取磁盘SN
        char disk_sn2[64];
        if (server_get_disk_sn(server_ptr, target_disk, disk_sn2, sizeof(disk_sn2)) == 0) {
            // 通过server_write_disk_by_sn写入
            char server_sn2[64];
            server_get_sn(server_ptr, server_sn2, sizeof(server_sn2));
            if (server_write_disk_by_sn(server_sn2, disk_sn2, 0, direct_write_data, strlen(direct_write_data)+1) == 0) {
                std::cout << "直接写入磁盘" << target_disk << "成功，SN: " << disk_sn2 << std::endl;
                // 再通过SN读取
                char read_buf2[64] = {0};
                if (server_read_disk_by_sn(server_sn2, disk_sn2, 0, read_buf2, strlen(direct_write_data)+1) == 0) {
                    std::cout << "通过SN读取磁盘" << target_disk << "成功，数据: " << read_buf2 << std::endl;
                    if (strcmp(read_buf2, direct_write_data) == 0) {
                        std::cout << "数据一致性验证通过！" << std::endl;
                    } else {
                        std::cout << "数据不一致，验证失败！" << std::endl;
                    }
                } else {
                    std::cout << "通过SN读取磁盘失败" << std::endl;
                }
            } else {
                std::cout << "直接写入磁盘失败" << std::endl;
            }
        } else {
            std::cout << "获取磁盘SN失败" << std::endl;
        }
    } else {
        std::cout << "磁盘数量不足，无法测试" << std::endl;
    }

    server_destroy(server);
    return 0;
} 